﻿using System;
using NUnit.Framework;
using RenewalProjects.Libraries.Common.Testing.Interfaces;
using RenewalProjects.Libraries.Wcf.EasyProxy.ChannelAllocators.Factories;
using RenewalProjects.Libraries.Wcf.EasyProxy.ChannelAllocators.Settings;
using RenewalProjects.Libraries.Wcf.EasyProxy.ChannelHolders;
using RenewalProjects.Libraries.Wcf.EasyProxy.Pools;
using RenewalProjects.Libraries.Wcf.EasyProxy.Tests.TestData;
using RenewalProjects.Libraries.Wcf.EasyProxy.Tests.TestBuilders;
using Rhino.Mocks;

namespace RenewalProjects.Libraries.Wcf.EasyProxy.Tests.Fixtures
{
	[TestFixture]
	public class PooledChannelResourceProviderFactoryFixture : IUsesRhinoMocks
	{
	    #region Properties
	    
	    public MockRepository Mock { get; set; }
	    
	    #endregion
	    
	    #region Test methods
	    
	    [SetUp]
	    public void Reset_factory_methods()
	    {
	        PooledChannelResourceProviderFactory<ITest>.ResetFactoryMethods();
	    }
	    
		[Test]
		public void SupplyReadyResourceItemProvider_called_expecting_listpool()
		{
			var helper = new ChannelFactoryBuilder<ITest>(Mock);
			
			Mock.ReplayAll();
			
			var result = PooledChannelResourceProviderFactory<ITest>.SupplyReadyResourceItemProvider(
				helper.GetFactory(),
				helper.GetServiceEndpoint(),
				new PoolChannelAllocatorSettings { ReadyChannelMax = 1 });

			Assert.IsNotNull(result);
			Assert.IsInstanceOf<ResourceListPool<ReadyChannelHolder<ITest>>>(result);
			Assert.IsNotNull(result.ResourceItemFactory);
		}
		
		[Test]
		public void SupplyReadyResourceItemProvider_called_not_expecting_listpool_for_max_is_0()
		{
			var helper = new ChannelFactoryBuilder<ITest>(Mock);
			
			Mock.ReplayAll();
			
			var result = PooledChannelResourceProviderFactory<ITest>.SupplyReadyResourceItemProvider(
				helper.GetFactory(),
				helper.GetServiceEndpoint(),
				new PoolChannelAllocatorSettings { ReadyChannelMax = 0 });

			Assert.IsNull(result);
		}
		
		[Test]
		public void SupplyStandbyResourceItemProvider_called_expecting_listpool()
		{
			var helper = new ChannelFactoryBuilder<ITest>(Mock);
			
			Mock.ReplayAll();
			
			var result = PooledChannelResourceProviderFactory<ITest>.SupplyStandbyResourceItemProvider(
				helper.GetFactory(),
				helper.GetServiceEndpoint(),
				new PoolChannelAllocatorSettings { StandbyChannelMax = 1 });

			Assert.IsNotNull(result);
			Assert.IsInstanceOf<ResourceListPool<StandbyChannelHolder<ITest>>>(result);
			Assert.IsNotNull(result.ResourceItemFactory);
		}
		
		[Test]
		public void SupplyStandbyResourceItemProvider_called_not_expecting_listpool_for_max_is_0()
		{
			var helper = new ChannelFactoryBuilder<ITest>(Mock);
			
			Mock.ReplayAll();
			
			var result = PooledChannelResourceProviderFactory<ITest>.SupplyStandbyResourceItemProvider(
				helper.GetFactory(),
				helper.GetServiceEndpoint(),
				new PoolChannelAllocatorSettings { StandbyChannelMax = 0 });

			Assert.IsNull(result);
		}
		
		#endregion
	}
}